package roomServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.soap.SOAPPart;
import java.io.IOException;
import java.sql.SQLException;

import static java.lang.System.out;

@WebServlet(urlPatterns = "/addRoom")
public class addRoom extends HttpServlet {
    java.sql.Connection conn = null;
    java.sql.Statement sqlStmt = null;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("UTF-8");

        String room_type=request.getParameter("room_type");
        String room_number=request.getParameter("room_number");
        String room_status=request.getParameter("room_status");
        int room_price=Integer.parseInt(request.getParameter("room_price"));

        response.setContentType("text/html;charset=UTF-8");

        System.out.println(request.getParameter("save"));

            try {
                Class.forName("org.h2.Driver");
                conn = java.sql.DriverManager.getConnection("jdbc:h2:~/jpa-demo-db;MODE=MYSQL;DB_CLOSE_DELAY=-1", "sa", "sa");
                sqlStmt = conn.createStatement();
                sqlStmt.executeUpdate(
                        "insert into room (style,rno,status,price)" + " values ('" + room_type + "','" + room_number + "','" + room_status + "','" + room_price + "')"
                );

            } catch (Exception ex) {
                out.println(ex.toString());
            }
            System.out.println("增加成功!");
            response.sendRedirect("html/hostel.jsp");
        }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
